package Data::Conveyor::App::Exported::Launcher;

# $Id: Launcher.pm 9617 2005-07-04 13:26:42Z ac $

use warnings;
use strict;
use Getopt::Long;
use IO::Handle;
use FindBin '$Bin';


our $VERSION = '0.02';


use base 'Exporter';


our @EXPORT = qw(start);


sub start {
    STDOUT->autoflush;
    STDERR->autoflush;

    my %opt;
    GetOptions(\%opt, qw/
         lockpath=s
         parallel=s
    /);
    usage() unless defined $opt{parallel} && $opt{lockpath};
    for (qw/PROJROOT CF_CONF PERL5OPT/) {
         die "$_ not set" unless $ENV{$_};
    }

    my @executable = (
      "$Bin/reg_dispatch.pl",
      "--lockpath=$opt{lockpath}"
    );

    require Data::Conveyor::Lock::Dispatcher;
    my $lockclass = Data::Conveyor::Lock::Dispatcher->new(
        lockpath => $opt{lockpath},
        numlocks => $opt{parallel}
    );
    $lockclass->administrate_locks;

    # we lose the lock here, of course. in the worst case
    # though, we just bloat and finally end up without the lock.

    if ($lockclass->get_lock) {
        exec $^X, @executable;
        die "exec @executable failed.";
    }
}


sub usage () {
   (my $exe = $0) =~ s|.*/||;
    printf STDERR <<'__EOF', $exe;

Usage:
 %s --lockpath=<directory> --parallel=<n>

__EOF
exit;
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

{% p.package %} - stage-based conveyor-belt-like ticket handling system

=head1 SYNOPSIS

    {% p.package %}->new;

=head1 DESCRIPTION

None yet. This is an early release; fully functional, but undocumented. The
next release will have more documentation.

=head1 METHODS

=over 4

{% p.write_methods %}

=back

{% p.write_inheritance %}

{% PROCESS standard_pod %}

=cut

